<?php

/*
 * group at_info 
 * @author yangchao
 * @package group
 * @copyright yangchao
 * @version 1.2
 * @code-encode utf-8
 * @data-encode utf-8
*/

if (isset($_GET["test_db"]))
{
	include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
}
define("CACHE_PREFIX_AT_INFO","_at_info_");
class db_at_info extends db_base
{
	var $tbl_name = "at_info";

	function db_at_info()
	{
		parent::db_base();
	}

//前台程序开始	
	/**
	 * 前台显示单个数据
	 * @param int id
	 */
	function get_item_by_id($id)
	{       
		//验证传入参数
		$id = intval($id);
		if(!$id) throw new Exception("DB:id错误！");
		//如果id是字符串，并且在where条件中使用 ：$id = string::esc_mysql($id);
			 
		$data = false;
		$key = CACHE_PREFIX_AT_INFO . "-get_item_by_id-{$id}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			//处理where条件
			$where = " id = {$id} and status = 1 " ;	
			$data = $this->get_alllist($this->tbl_name, 1, 1, "",$where);
			$mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$data = unserialize($cache);
		}

		return $data[0];
	}	
	
	
	//根据uid取个数
	function get_count_by_uid($uid)
	{       
		//验证传入参数
		$uid = intval($uid);
		if(!$uid) throw new Exception("DB:uid错误！");
		//如果id是字符串，并且在where条件中使用 ：$id = string::esc_mysql($id);
			 
		$data = false;
		$key = CACHE_PREFIX_AT_INFO . "-get_count_by_uid-{$uid}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			//处理where条件
			$where = " to_uid = {$uid} and status = 1 " ;	
			$data = $this->get_listcount($this->tbl_name, $where);
			$mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$data = unserialize($cache);
		}

		return $data;
	}	
	
	
	//根据uid取列表
	function get_list_by_uid($uid,$p=1,$pcount=20)
	{       
		//验证传入参数
		$uid = intval($uid);
		$p = intval($p);
		$pcount = intval($pcount);
		if(!$uid) throw new Exception("DB:uid错误！");
		//如果id是字符串，并且在where条件中使用 ：$id = string::esc_mysql($id);
			 
		$data = false;
		$key = CACHE_PREFIX_AT_INFO . "-get_list_by_uid-{$uid}-{$p}-{$pcount}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			//处理where条件
			$where = " to_uid = {$uid} and status = 1 " ;	
			$data = $this->get_alllist($this->tbl_name, $p, $pcount, "id desc ",$where);
			$mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$data = unserialize($cache);
		}

		return $data;
	}	
	
	
	//用户未读@消息
	function get_at_count_unread($uid,$id)
	{       
		//验证传入参数
		$id = intval($id);
		$uid = intval($uid);
		
		if(!$uid) throw new Exception("DB:uid错误！");
		//如果id是字符串，并且在where条件中使用 ：$id = string::esc_mysql($id);
			 
		$data = false;
		$key = CACHE_PREFIX_AT_INFO. "-get_at_count_unread-{$uid}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			//处理where条件
			$where = " to_uid = {$uid} and id > {$id} and status = 1 " ;	
			$data = $this->get_listcount($this->tbl_name,$where);
			$mc->set($key, serialize($data), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$data = unserialize($cache);
		}

		return $data;
	}	
	
	
		
	//添加一条数据
	function insert_at_info(
		$current_uid,
		$to_uid,
		$from_id,
		$at_type
		
	)
	{
		global $config;
		//详细字段
		$ary = array();
		$ary["to_uid"] = $to_uid;
		$ary["from_id"] = $from_id;
		$ary["at_type"] = $at_type;
		$ary["from_uid"] = $current_uid;
		$ary["status"] = 1;
		
		$ret = $this->insert($this->tbl_name, $ary);
		if ($ret)
		{
			$memcache = mem_cache::get_instance();
			$key[] = "need to delete";
			$memcache->delete_ary($key);
		}
		else
		{
			return false;
		}

		return $this->get_lastinsertid();
	}

	

//前台程序结束
	

	
//后台程序开始	
	/**
	 * 后台取单个数据
	 * @param int id
	 */
	function admin_get_item_by_id($id)
	{       
		//验证传入参数
		$id = intval($id);
		if(!$id) throw new Exception("DB:id错误！");
		//如果$id是字符串，并且在where条件中使用 ：$id = string::esc_mysql($id);
		
		//处理where条件
		$where = " id = {$id} and status = 1 " ;	
		$data = $this->get_alllist($this->tbl_name, 1, 1, "",$where);
		
		return $data[0];
	}	
	
}


